﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Threading.Tasks;
using System.Data.SqlClient;
using DAL;

namespace BLL
{
    public class ForRenew
    {
        public SqlDataReader ReBorrow(int BookID)
        {
            //查询续借书籍信息
            String sql = string.Format(@"select Reader.ReaderID,ReaderName,Books.BookID,Book.BookName,BorrowDate,ReturnDate,DATEADD(day,20,ReturnDate)
                                        from Reader,Books,Book,Borrow where Reader.ReaderID = Borrow.ReaderID 
                                        and Book.ID = Books.ID and Borrow.BookID = Books.BookID and Books.BookID = {0} and GiveBackDate is null", BookID);

            SqlDataReader reader = DBHelper.ExecuteReader(sql);
            return reader;
        }
        public bool ReNewBook(string ReturnDate,string BookID,int ReaderID)
        {
            bool valid = false;

            //更新续借日期，并且根据归还日期为空，图书卡号，读者ID号 ，
            string sql = string.Format(@"update Borrow set ReturnDate = '{0}' where BookID = {1} and ReaderID = {2} and GiveBackDate is null;", ReturnDate, BookID, ReaderID);

            int iRet = DBHelper.ExecuteNonQuery(sql);

            if (iRet == 1)
            {
                valid = true;
            }
            return valid;
        }
        public bool IsReNew(string BookID,string ReaderID)
        {
            //默认不可续借
            bool valid = false;

            //查询归还日期为空，并且根据图书卡号，读者ID号 ，应还和借阅日期之差小于 80 天可续借，
            string sql = string.Format(@"select count(*) from Borrow where DATEDIFF(day,BorrowDate,ReturnDate) < 80 and BookID = {0} and ReaderID = {1} and GiveBackDate is null;",
                BookID, ReaderID);

            //执行
            if (Convert.ToInt32(DBHelper.ExecuteScalar(sql)) == 1)
            {
                //可续借
                valid = true;
            }

            return valid;
        }
    }
}
